#define PI 3.14

uniform sampler2D   occludersMap;
uniform float       textureSize;
uniform float       heightRatio;

// Threshold to be considered occluder
uniform float       alphaThreshold;

void main () {
    float distance = 1.0;
    
    // polar to rectangular coordinates
    // s coord corresponds to angle value
    // it goes like this :
    //   0    0.25    0.5    0.75     1
    //  -1    -0.5     0      0.5     1
    //  -PI   -PI/2    0     PI/2    PI
    //
    // BUT because of SFML the Y axis is flipped
    // we need to compute it like it was 
    //  PI    PI/2     0     -PI/2    -PI
    float theta = -PI * (gl_TexCoord[0].s * 2.0 + 1.0);
    float cosT = cos( theta );
    float sinT = sin( theta );
    
    // height ratio
    float rPrevPrev     = 0.0;
    float rPrev         = heightRatio;
    float r;
    
    // cartesian coordinates
    vec2 cartCoordPrevPrev  = vec2( rPrevPrev * cosT, rPrevPrev * sinT ) * 0.5 + 0.5;
    vec2 cartCoordPrev      = vec2( rPrev * cosT, rPrev * sinT ) * 0.5 + 0.5;
    vec2 cartCoord;
    
    // color
    vec4 colorPrevPrev  = texture2D( occludersMap, cartCoordPrevPrev );
    vec4 colorPrev      = texture2D( occludersMap, cartCoordPrev );
    vec4 color;
    
    for ( float y = 2.0; y < textureSize; y += 1.0 ) {
        r = y * heightRatio;
        
        // get the cartesian coordinates
        // coord have value between [-1,1] -> [0,1]
        cartCoord = vec2( r * cosT, r * sinT ) * 0.5 + 0.5;
        
        // get the color
        color = texture2D( occludersMap, cartCoord );
        
        if ( colorPrevPrev.a > alphaThreshold && colorPrev.a <= alphaThreshold && color.a <= alphaThreshold ) {
            distance = min( distance, rPrevPrev );
            break; // first occluder found, no need to go further
        }
        
        rPrevPrev           = rPrev;
        rPrev               = r;
        
        cartCoordPrevPrev   = cartCoordPrev;
        cartCoordPrev       = cartCoord;
        
        colorPrevPrev       = colorPrev;
        colorPrev           = color;
    }
    
    gl_FragColor = distance;
}
/*
void main () {
    float distance = 1.0;
    
    // polar to rectangular coordinates
    // s coord corresponds to angle value
    // it goes like this :
    //   0    0.25    0.5    0.75     1
    //  -1    -0.5     0      0.5     1
    //  -PI   -PI/2    0     PI/2    PI
    //
    // BUT because of SFML the Y axis is flipped
    // we need to compute it like it was 
    //  PI    PI/2     0     -PI/2    -PI
    float theta = -PI * (gl_TexCoord[0].s * 2.0 + 1.0);
    float cosT = cos( theta );
    float sinT = sin( theta );

    float rPrevPrev = gl_TexCoord[0].t - 2.0 * heightRatio;
    float rPrev     = gl_TexCoord[0].t - heightRatio;
    float r         = gl_TexCoord[0].t;
        
    // get the cartesian coordinates
    // coord have value between [-1,1] -> [0,1]
    vec2 cartCoordPrevPrev  = vec2( rPrevPrev * cosT, rPrevPrev * sinT ) * 0.5 + 0.5;
    vec2 cartCoordPrev      = vec2( rPrev * cosT, rPrev * sinT ) * 0.5 + 0.5;
    vec2 cartCoord          = vec2( r * cosT, r * sinT ) * 0.5 + 0.5;
    
    // get the color
    vec4 colorPrevPrev  = texture2D( occludersMap, cartCoordPrevPrev );
    vec4 colorPrev      = texture2D( occludersMap, cartCoordPrev );
    vec4 color          = texture2D( occludersMap, cartCoord );
    
    if ( colorPrevPrev.a > alphaThreshold && colorPrev.a <= alphaThreshold && color.a <= alphaThreshold ) {
        distance = min( distance, rPrevPrev );
    }
    
    // gl_FragColor = distance;
    gl_FragColor = color;
}
*/